package com.itbour.service.trade.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.itbour.com.page.JspPage;
import com.itbour.com.page.Page;
import com.itbour.common.util.StringUtils;
import com.itbour.dao.trade.OrdersDao;
import com.itbour.dto.trade.ExpTrackInfoDto;
import com.itbour.dto.trade.ExpressComInfoDto;
import com.itbour.dto.trade.OrderDetailInfoDto;
import com.itbour.dto.trade.OrderInfoDto;
import com.itbour.dto.trade.PartnerInfoDto;
import com.itbour.dto.trade.StateHisInfoDto;
import com.itbour.service.trade.OrdersService;


/**
* 订单
* 
* copyright   itbour team 2017
* @author     xc
*
*/

public class OrdersServiceImpl implements OrdersService {

	private OrdersDao ordersDao;
	
	/**
	 * @param countKey
	 *            查询该表所有记录行的SQL语句ID
	 * @param sqlKey
	 *            需要查询记录的SQL语句ID
	 * @param param
	 *            传递给查询语句的参数
	 * @param pageNo
	 *            需要查询的实际页
	 * @param pageSize
	 *            每页记录行数
	 * @return page对象
	 */
	public Page ordersPgQuery(Map<String,Object> param, Integer pageNo, Integer pageSize) {
		try {
			Page result = null;
			
			// 实际起始位置
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = ordersDao.selectOrdersCnt(param);
			
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			
			// 分页检索
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<OrderInfoDto> data = ordersDao.selectOrdersInfo(param);

			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
			//clearSelectDataSource(dataSource);
		}
	}

	public List<OrderInfoDto> selectOrdersInfo(Map<String,Object> param) {
		return ordersDao.selectOrdersInfo(param);
	}
	
	public void delOrder(String delIds) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("order_ids", StringUtils.listToString2(StringUtils.stringToList2(delIds)));
		ordersDao.deleteOrderInfoLogic(param);
		ordersDao.deleteDetailInfoLogic(param);// 一并删除明细信息
	}

	public void addOrder(OrderInfoDto orderInfo) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("order_no", orderInfo.getOrder_no());
		param.put("inv_id", orderInfo.getInv_id());
		param.put("partner_id", orderInfo.getPartner_id());
		param.put("pay_platform", orderInfo.getPay_platform());
		param.put("desc", orderInfo.getDesc());
		param.put("state", orderInfo.getState());
		param.put("total_fee", orderInfo.getTotal_fee());
		param.put("addr_id", orderInfo.getAddr_id());
		param.put("user_id", orderInfo.getUser_id());
		param.put("update_by", orderInfo.getUpdate_by());
		ordersDao.insertOrderInfo(param);
	}
	
	
	// 明细列表
	public Page detailsPgQuery(Map<String,Object> param, Integer pageNo, Integer pageSize) {
		try {
			Page result = null;
			
			// 实际起始位置
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = ordersDao.selectDetailsCnt(param);
			
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			
			// 分页检索
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<OrderDetailInfoDto> data = ordersDao.selectDetailsInfo(param);

			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
			//clearSelectDataSource(dataSource);
		}
	}

	public List<OrderDetailInfoDto> selectDetailsInfo(Map<String,Object> param) {
		return ordersDao.selectDetailsInfo(param);
	}
	
	public void delDetail(String delIds) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("ids", StringUtils.listToString2(StringUtils.stringToList2(delIds)));
		ordersDao.deleteDetailInfoLogic(param);
	}

	public void addDetail(OrderDetailInfoDto detailInfo) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("order_id", detailInfo.getOrder_id());
		param.put("item_id", detailInfo.getItem_id());
		param.put("pp_id", detailInfo.getPp_id());
		param.put("unit", detailInfo.getUnit());
		param.put("qty", detailInfo.getQty());
		param.put("user_doc_id", detailInfo.getUser_doc_id());
		param.put("state", detailInfo.getState());
		param.put("fee", detailInfo.getFee());
		param.put("desc", detailInfo.getDesc());
		param.put("update_by", detailInfo.getUpdate_by());
		ordersDao.insertDetailInfo(param);
	}
	
	public void updDetail(Map<String,Object> param) {
		ordersDao.updateDetailInfoById(param);
	}
	
	public void updOrder(Map<String,Object> param) {
		ordersDao.updateOrderInfoById(param);
	}

	public List<PartnerInfoDto> selectPartnerInfo(Map<String,Object> param){
		return ordersDao.selectPartnerInfo(param);
	}
	
	// 订单状态历史
	public void addStateHis(StateHisInfoDto stateHisInfo){
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("trade_obj_id", stateHisInfo.getTrade_obj_id());
		param.put("state", stateHisInfo.getState());
		param.put("total_fee", stateHisInfo.getTotal_fee());
		param.put("user_id", stateHisInfo.getUser_id());
		param.put("type", stateHisInfo.getType());
		param.put("desc", stateHisInfo.getDesc());
		param.put("update_by", stateHisInfo.getUpdate_by());
		ordersDao.insertStateHisInfo(param);
	}
	
	// 用户通知
	public void addNotifyUserRecd(Map<String,Object> param){
		ordersDao.insertNotifyUserlInfo(param);
	}
	
	public OrdersDao getOrdersDao() {
		return ordersDao;
	}

	public void setOrdersDao(OrdersDao ordersDao) {
		this.ordersDao = ordersDao;
	}

	//快递分类
	public List<ExpressComInfoDto> getExpComInfo(Map<String, Object> parm) {
		return ordersDao.getExpComInfo(parm);
	}

	/**本地DB物流信息
	 */
	public Page getLogisticsInfo(Map<String, Object> parm, Integer pageNo,
			Integer pageSize) {
		try {
			Page result = null;
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = ordersDao.getLogisticsCnt(parm);//获取物流信息 个数
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			parm.put("pageSize", pageSize);
			parm.put("skipResults", skipResults);
			List<ExpTrackInfoDto> data = ordersDao.getLogicsticsInfo(parm);//获取本地物流信息
			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
			//clearSelectDataSource(dataSource);
		}
	}
	
	/**
	 * 更新本地物流信息data的数据
	 */
	public boolean updExpInfoData(Map<String, Object> parm) {
		try{
			ordersDao.updExpInfoData(parm);
			return true;
		}catch(Exception e){
			System.out.println(e.getMessage());
			return false;
		}
	}

	//查询本地物流信息
	public List<ExpTrackInfoDto> getExpTrackInfo(Map<String, Object> parm) {
		return ordersDao.getLogicsticsInfo(parm);//获取本地物流信息;
	}

	
	//插入本地物流信息
	public void insertExpTrackInfo(Map<String, Object> parm) {
		ordersDao.insertExpTrackInfo(parm);
	}

}
